package com.lxblyw.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.lxblyw.entity.Contact;
import com.lxblyw.util.DBHelp;
import com.lxblyw.util.RowMapper;

public class ContactDao {
	DBHelp<Contact> db = new DBHelp<Contact>();

	public void saveContact(Contact contact) {
		String sql = "insert into t_contact(id,name,companyname,mobile,tel,email,address,website,webo,content,createtime,projectid) values(?,?,?,?,?,?,?,?,?,?,?,?)";
		db.executeSQL(sql, contact.getId(),contact.getName(),contact.getCompanyname(),contact.getMobile(),contact.getTel(),contact.getEmail(),contact.getAddress(),contact.getWebsite(),contact.getWeibo(),contact.getContent(),contact.getCreatetime(),contact.getProjectid());
	}
	
	private class ConatactRowMapper implements RowMapper<Contact> {

		public Contact mapperRow(ResultSet rs) throws SQLException {
			Contact contact = new Contact();
			contact.setAddress(rs.getString("address"));
			contact.setCompanyname(rs.getString("companyname"));
			contact.setContent(rs.getString("content"));
			contact.setCreatetime(rs.getString("createtime"));
			contact.setEmail(rs.getString("email"));
			contact.setId(rs.getString("id"));
			contact.setMobile(rs.getString("mobile"));
			contact.setName(rs.getString("name"));
			contact.setProjectid(rs.getString("projectid"));
			contact.setTel(rs.getString("tel"));
			contact.setWebsite(rs.getString("website"));
			contact.setWeibo(rs.getString("webo"));
			
			return contact;
		}
		
	}

	public List<Contact> findAllByProjectId(String projectid) {
		String sql = "select id,name,companyname,mobile,tel,email,address,website,webo,content,createtime,projectid from t_contact where projectid=?";
		return db.executeQueryForList(sql, new ConatactRowMapper(), projectid);
	}

	public Contact findContact(String contactid) {
		String sql = "select id,name,companyname,mobile,tel,email,address,website,webo,content,createtime,projectid from t_contact where id=?";
		return db.executeQueryForObject(sql, new ConatactRowMapper(), contactid);
	}

	public void delContact(String contactid) {
		String sql = "delete from t_contact where id=?";
		db.executeSQL(sql, contactid);
	}
}
